<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>代金券包管理列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="plugins/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="//at.alicdn.com/t/font_tnyc012u2rlwstt9.css" media="all"/>
    <link rel="stylesheet" href="../../css/news.css" media="all"/>
</head>
<body class="childrenBody">
<blockquote class="layui-elem-quote news_search">
    <form class="layui-form">
        <div class="layui-inline">
            <label class="layui-form-label">券包名称</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" name="name" placeholder="请输入代金券包名称">
            </div>
        </div>
        <div class="layui-inline">
            <a class="layui-btn search_btn" lay-submit="" lay-filter="queryVoucherPackage">查询</a>
        </div>

        <div class="layui-inline">
            <a class="layui-btn layui-btn-normal voucherPackagesAdd_btn">添加券包</a>
        </div>
    </form>

</blockquote>
<div class="layui-form news_list">
    <table class="layui-table" id="voucherPackageList" lay-filter="voucherPackages"></table>
</div>
<div id="page"></div>
<script type="text/javascript" src="js/jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="plugins/layui/layui.js"></script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit"><i class="iconfont icon-edit"></i>详情</a>
    <a class="layui-btn layui-btn-default layui-btn-xs" lay-event="sendVoucher"><i class="iconfont icon-edit"></i>发券</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete"><i class="layui-icon"></i>删除</a>
</script>
<script type="text/javascript">
    layui.config({
        base: '/js/admin/'
    }).extend({
        ajaxExtention: 'ajaxExtention',//加载自定义扩展，每个业务js都需要加载这个ajax扩展
        $tool: 'tool'
    }).use(['table', 'form', '$tool'], function () {
        var table = layui.table;
            form = layui.form,
            $tool = layui.$tool;
        //方法级渲染
        var tableIns = table.render({
            elem: '#voucherPackageList'
            , url: '/voucherPackages'
            , cols: [[
                {field: 'id', title: 'ID', fixed: true}
                , {field: 'name', title: '卡券包名称'}
                , {field: 'amount', title: '卡券面值'}
                , {field: 'quantity', title: '卡券数量'}
                , {field: 'dueTime', title: '到期时间'}
                , {
                    field: 'createTime', title: '创建时间', templet: function (d) {
                        return $tool.formatDate(d.createTime);
                    }
                }
                , {title: '操作', width: '20%', align: 'center', toolbar: '#barDemo'}
            ]]
            , id: 'testReload'
            , page: true
        });

        form.on("submit(queryVoucherPackage)", function (data) {
            var name = data.field.name;
            tableIns.reload({
                where: {
                    name: name
                }
            });
            return false;
        });



        table.on('tool(voucherPackages)', function (obj) {
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var dataId = obj.data.id;
            if (layEvent === 'del') { //删除
                layer.confirm('真的删除该代金券包吗', function (index) {
                    layer.close(index);
                    $.ajax({
                        type: "DELETE",
                        url: "/voucherPackages/delete/" + dataId,
                        success: function (res) {
                            if (res.code == 2000) {
                                obj.del();
                                top.layer.close(index);
                                top.layer.msg("删除成功");
                            }
                        },
                        error: function (res) {
                            alert("请求错误");
                        }
                    });
                });
            } else if (layEvent === 'edit') { //编辑
                editVoucherPackage(dataId);
            }else if(layEvent === 'sendVoucher'){
                sendVoucher(dataId);
            }
        });
    });

    function sendVoucher(id){
        var index = layui.layer.open({
            title: "批量发券",
            type: 2,
            content: "voucher_packages/voucherPackagesSend.html?id="+id
        });
        $(window).resize(function () {
            layui.layer.full(index);
        });
        layui.layer.full(index);
    }
    $(".voucherPackagesAdd_btn").click(function () {
        var index = layui.layer.open({
            title: "添加代金券包",
            type: 2,
            area: ['840px', '420px'], //宽高
            content: "voucher_packages/voucherPackagesAdd.html"
        })
    })


    function editVoucherPackage(id) {
        var index = layui.layer.open({
            title: "编辑代金券包",
            type: 2,
            area: ['840px', '420px'], //宽高
            content: "voucher_packages/voucherPackagesEdit.html?id=" + id
        });
    }
</script>
</body>
</html>